package br.com.ruidobrasil.util.componentes.slides {
	
	import br.com.ruidobrasil.util.TratamentoErros;
	import br.com.ruidobrasil.vo.PublicacaoVO;
	
	import flash.display.Bitmap;
	import flash.display.Loader;
	import flash.display.LoaderInfo;
	import flash.events.Event;
	import flash.events.IOErrorEvent;
	import flash.events.TimerEvent;
	import flash.utils.Timer;
	
	import mx.collections.ArrayCollection;
	import mx.controls.Alert;
	import mx.controls.Label;
	import mx.events.FlexEvent;
	import mx.rpc.events.FaultEvent;
	import mx.rpc.events.ResultEvent;
	
	public class Slides extends SlidesView {
		
		import mx.events.EffectEvent;
		import mx.effects.easing.*;
		
		private var timer: Timer = new Timer(25000);
		private var numeroSlide: int = 1;
		private var listaSlides: ArrayCollection;
		private var vo: PublicacaoVO;
		private var loader: Loader;
		
		public function Slides() {
			super();
			this.addEventListener(FlexEvent.CREATION_COMPLETE, init);
		}
		
		public function init(event: FlexEvent): void {
			
			rm.obterSlides.addEventListener(ResultEvent.RESULT, resultObterSlides);
			rm.obterSlides.addEventListener(FaultEvent.FAULT, TratamentoErros.fault);
			
			timer.addEventListener(TimerEvent.TIMER, mudarSlide);
			imgSlides.addEventListener("completeEffect", finalizouEfeitoSlide);
			this.addEventListener(BotaoSlideEvent.BOTAO_SLIDE_EVENT, carregarSlide);
			
			rm.obterSlides();
			
			imgSlides.visible = false;
			
			loader = new Loader();
			loader.contentLoaderInfo.addEventListener(Event.COMPLETE, onLoadComplete, false, 0, true);
			loader.contentLoaderInfo.addEventListener(IOErrorEvent.IO_ERROR, onLoadError);
			
			/* imgSlides.visible = false;
			imgSlides.source = "slides/slide" + numeroSlide + ".2.jpg";
			imgSlides.visible = true;
			
			carregarInformacoesSlide();
			
			numeroSlide = 2;
			timer.start(); */
		}
		
		private function resultObterSlides(event: ResultEvent): void {
			listaSlides = event.result.dados.lista;
			
			vo = listaSlides.getItemIndex(0) as PublicacaoVO;
			loader.loadBytes(vo.imagemPublicacao);
			
			//var loaderInfo: LoaderInfo = new LoaderInfo(vo.idImagemPublicacao);
			//var bitMap: Bitmap = loaderInfo.loader.content as Bitmap;
			//imgSlides.source = bitMap;
		}
		
		private function onLoadComplete(e: Event): void {
			var loaderInfo: LoaderInfo = e.target as LoaderInfo;
			var bitMap: Bitmap = loaderInfo.loader.content as Bitmap;
			imgSlides.source = bitMap;
			imgSlides.visible = true;
			
			carregarInformacoesSlide();

			/*
			var child: Image = this.cnvImagem.getChildByName(loaderInfo.loader.mask.name) as Image;
			
			if (child == null) {
				
				var lbl: Label = new Label();
				lbl.text = "Aguarde... Carregando a imagem...";
				this.cnvImagem.addChild(lbl);
				
			} else {
				var index: int = this.cnvImagem.getChildIndex(child);
				this.cnvImagem.removeChildAt(index);
				this.cnvImagem.addChildAt(img, index);
			}
			*/
		}
		
		private function onLoadError(e: Event): void {
			Alert.show("Erro na exibição da imagem da assinatura.", "Erro!");
		}
		
		private function carregarInformacoesSlide(): void {
			lblTitulo.text = vo.tituloPublicacao;
			txtResumo.text = vo.resumoPublicacao;
		}
		
		private function carregarSlide(event: BotaoSlideEvent): void {
			imgSlides.visible = false;
			imgSlides.source = "slides/slide" + event.numeroBotao + ".2.jpg";
			imgSlides.visible = true;
			numeroSlide = event.numeroBotao;
			carregarInformacoesSlide();
			
			if (numeroSlide >= 3) {
				numeroSlide = 1;
			} else {
				numeroSlide++;
			}
			
			timer.reset();
			timer.start();
		}
		
		private function mudarSlide(event: TimerEvent): void {
			imgSlides.visible = false;
			imgSlides.source = "slides/slide" + numeroSlide + ".2.jpg";
			imgSlides.visible = true;
			carregarInformacoesSlide();
			
			if (numeroSlide >= 3) {
				numeroSlide = 1;
			} else {
				numeroSlide++;
			}
		}
		
		
		
		private function finalizouEfeitoSlide(event: EffectEvent): void {
			timer.start();
		}
	}
}